import java.text.SimpleDateFormat

class ManagerController {

    def index = { }
    def build = {
        def day = 100
        if (params.dayz) {
            day = params.dayz
        }
//        def s = Student.random(10).list()
//        def t = Teacher.random(10).list()
//        def s = Student.withCriteria {
//
//            firstResult((int)(Math.random()*100+1))
//            maxResults(10)
//         }
//
//        def t = Teacher.withCriteria {
//            firstResult((int)(Math.random()*100+1))
//            maxResults(10)
//        }
        def s = Student.list(sort: "createTime", order: "desc", max: 10 , offset: (int)(Math.random()*300+1))
        def t = Teacher.list(sort: "createTime", order: "desc",max: 10 , offset: (int)(Math.random()*500+1))
        def p = Post.list(sort: "createTime", order: "desc",max: 10 , offset: (int)(Math.random()*100+1))
        [s : s , t : t , p: p]
    }
    def update = {
        flash.message = ''
        def newdate 
        try{
            def dateFormat = new SimpleDateFormat ("yyyy-MM-dd"); 
            newdate = dateFormat.parse(params.newdate)
     
        }catch(Exception e){println "data error"}
        if (!newdate) {
            newdate = new Date()
        }

        
        params.s.each{
            if (it) {
                def s = Student.get(it)
                s.createTime = newdate
                if(s.save(flush: true)){
                    flash.message += "save " + s + "<br>"
                }
            }
        } 
        params.t.each{
            if (it) {
                def s = Teacher.get(it)
                s.createTime = newdate
                if(s.save(flush: true)){
                    flash.message += "save " + s + "<br>"
                }
            }
        } 
        params.p.each{
            if (it) {
                def s = Post.get(it)
                s.createTime = newdate
                if(s.save(flush: true)){
                    flash.message += "save " + s + "<br>"
                }
            }
        } 
        redirect(action: "build")
    }
}
